import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus_seven/apis/home.dart';
import 'package:flutter_enjoy_plus_seven/pages/home/components/home_list.dart';
import 'package:flutter_enjoy_plus_seven/pages/home/components/home_nav.dart';
import 'package:flutter_enjoy_plus_seven/utils/TokenManager.dart';
import 'package:flutter_enjoy_plus_seven/utils/toast.dart';
import 'package:image_picker/image_picker.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List announcementList = [];

  Timer? timer;
  int countDown = 60;

  @override
  void initState() {
    super.initState();
    getAnnouncementList();
  }

  // 获取社区公告
  getAnnouncementList() async {
    // 请求社区公告列表
    final res = await HomeApi.getAnnouncementList();
    // PromptAction.showSuccess(res.toString());
    // 更新社区公告列表
    setState(() {
      announcementList = res;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: const Color.fromARGB(40, 85, 145, 175),
        appBar: AppBar(
          title: const Text('享+社区'),
          backgroundColor: Colors.transparent,
          centerTitle: true,
        ),
        body: ListView(
          scrollDirection: Axis.vertical,
          padding: const EdgeInsets.all(10),
          children: [
            Row(
              children: [
                TextButton(
                  onPressed: () {
                    Navigator.pushNamed(context, '/add_house');
                  },
                  child: const Text('添加房屋'),
                ),
                TextButton(
                  onPressed: () {
                    Navigator.pushNamed(context, '/abc');
                  },
                  child: const Text('跳转不存在的页面'),
                ),
                TextButton(
                  onPressed: () async {
                    try {
                      // 图片选择器
                      final ImagePicker picker = ImagePicker();
                      final image =
                          await picker.pickImage(source: ImageSource.gallery);
                      PromptAction.showSuccess(image?.path ?? '');
                    } catch (e) {
                      PromptAction.showSuccess(e.toString());
                    }
                  },
                  child: const Text('图片选择器'),
                ),
              ],
            ),
            Row(
              children: [
                TextButton(
                  onPressed: () {
                    // 通用弹窗
                    showDialog(
                      context: context,
                      builder: (context) {
                        // 自定义弹窗内容
                        return AlertDialog(
                          title: const Text('测试弹窗'), // 标题
                          content: const Text('测试弹窗内容'), // 内容
                          // 按钮组
                          actions: [
                            TextButton(
                              onPressed: () {},
                              child: const Text('取消'),
                            ),
                            TextButton(
                              onPressed: () {},
                              child: const Text('确定'),
                            )
                          ],
                        );
                      },
                    );
                  },
                  child: const Text('测试弹窗'),
                ),
                TextButton(
                  onPressed: () {
                    // 半模态弹窗 - 底部弹窗
                    showBottomSheet(
                      enableDrag: true,
                      context: context,
                      builder: (context) {
                        return Container(
                          height: 200,
                          width: double.infinity,
                          decoration: const BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.only(
                              topLeft: Radius.circular(20),
                              topRight: Radius.circular(20),
                            ),
                          ),
                          child: const Column(
                            children: [
                              Text('弹窗2'),
                            ],
                          ),
                        );
                      },
                    );
                  },
                  child: const Text('弹窗2'),
                ),
                TextButton(
                  onPressed: () {
                    // 日期选择器
                    showDatePicker(
                      context: context,
                      firstDate: DateTime(2000, 1, 1),
                      lastDate: DateTime(2030, 12, 31),
                      initialDate: DateTime.now(),
                      onDatePickerModeChange: (DatePickerEntryMode mode) {
                        print(mode.toString());
                      },
                    );
                  },
                  child: const Text('弹窗3'),
                )
              ],
            ),
            Row(
              children: [
                TextButton(
                  onPressed: () {
                    tokenManager.setToken('1234567890', 'asdfghjkl');
                  },
                  child: const Text('持久化存储-存'),
                ),
                TextButton(
                  onPressed: () async {
                    final token = await tokenManager.getToken();
                    PromptAction.showSuccess(token);
                  },
                  child: const Text('持久化存储-取'),
                ),
                TextButton(
                  onPressed: () {
                    tokenManager.deleteToken();
                  },
                  child: const Text('持久化存储-删'),
                ),
                TextButton(
                  onPressed: () {
                    Navigator.pushNamed(context, '/login');
                  },
                  child: const Text('跳转登录页'),
                ),
                TextButton(
                  onPressed: () {
                    Navigator.pushNamed(context, '/profile');
                  },
                  child: const Text('跳转个人信息页'),
                ),
              ],
            ),
            Row(
              children: [
                TextButton(
                  onPressed: () {
                    timer = Timer.periodic(const Duration(seconds: 1), (timer) {
                      setState(() {
                        countDown--;
                        if (countDown <= 0) {
                          timer.cancel();
                          countDown = 60;
                        }
                      });
                    });
                  },
                  child: const Text('开启定时器'),
                ),
                Text('秒数:$countDown'),
                TextButton(
                  onPressed: () {
                    timer?.cancel();
                  },
                  child: const Text('停止定时器'),
                ),
              ],
            ),
            // 导航条
            HomeNav(),
            const SizedBox(height: 10),
            // 中间广告图
            ClipRRect(
              borderRadius: BorderRadius.circular(10.0),
              child: Image.asset('assets/images/banner@2x.jpg'),
            ),
            // 社区公告，数据父传子
            HomeList(notifyList: announcementList)
          ],
        ));
  }
}
